function Crop(id, tile)
{
    //unique only in hero crops[] list !
    this.id = id;

    this.pos = {
        row: findRowCol(tile).row,
        col: findRowCol(tile).col
    };

    this.type = null;
    this.heroId = null;
    this.sprites = [];
    this.curSprite = null;
    this.curState = 0;
    this.isWithering = false;

    this.cost = null;

    // --- TIMERS ---
    this.timerHandler = false;
    this.timerStartTime = null;
    this.timeRemaining = null;
    this.growTime = null;

    //amount of harvest
    this.productivity = null;
    //time before decaying
    this.storability = null;
    //sell price
    this.seedPrice = null;
}

//init crop client side from srv data
function initCrop(crop, cropTile) {
    var newCrop = null;

    switch(crop.type) {
        case CROPS.TOMATO:  newCrop = new Tomato(-1, cropTile); break;
        case CROPS.CORN:    newCrop = new Corn(-1, cropTile); break;
        case CROPS.WHEAT:   newCrop = new Wheat(-1, cropTile); break;
        default: break;
    }

    newCrop.curState = crop.curState;
    newCrop.heroId = crop.heroId;
    newCrop.timeRemaining = crop.timeRemaining;

    if(newCrop.curState > newCrop.sprites.length + 2) {
        newCrop.isWithering = true;
        newCrop.curSprite = c_decay;
    }

    cropTile.initCrop(newCrop);

    return newCrop;
}

function findCropByRowCol(row,col) {
    var crop = findTileByRowCol(row,col).crop;
    return crop;
}

Crop.prototype.getInfos = function() {
    var infosText = '<br />Crop stats :';
    var growStateHTML = null;

    selectedCrop = this;

    //tant que la crop grow
    if(this.curState <= 3)
    {
        growStateHTML = ( ' <br /> &nbsp;&nbsp;&nbsp;Grow state : ' +(this.curState+1) + ' / 5' );
    }
    //si elle est dans son last state, mais pas en decay
    else if(this.curState < 8)
    {
        growStateHTML = '<br/> You can havest this crop !<br/>';
        growStateHTML += '&nbsp;&nbsp;<button onclick="harvestAndSell()">Harvest and sell</button><br/>';
        growStateHTML += '&nbsp;&nbsp;<button onclick="storingState()">Harvest and store</button><br/>';
    }
    //si la crop decay
    else
    {
        growStateHTML = '<br /> &nbsp;&nbsp;&nbsp;Grow state : decaying... :(';
        growStateHTML += '<br/><button onclick="destroyCrop()">Destroy</button> ';
    }

    infosText += '' + growStateHTML;
    return infosText;
}

Crop.prototype.setupCrop = function() {
    this.heroId = hero.id;
//    console.log('init crop with pos : '+this.pos.row+','+this.pos.col);

    hero.addCrop(this.asSimpleObject());
    hero.addComplexCrop(this);

    var cropTile = findTileByRowCol(this.pos.row,this.pos.col);
        cropTile.setCrop(this);
        cropTile.drawTile();

    cl_crop_update(this.asSimpleObject());
    this.startTimer();
};

Crop.prototype.drawCrop = function() {
    var tmpTile = findTileByRowCol(this.pos.row,this.pos.col);

    if(this.curState < this.sprites.length)
        this.curSprite = this.sprites[this.curState];
    else if(this.curState < this.sprites.length + 2)
        this.curSprite = this.sprites[4];

    c.drawImage(this.curSprite,
        tmpTile.x + camera.x,
        tmpTile.y + camera.y);
}

Crop.prototype.startTimer = function() {
    var curCrop = this;

    if(this.timeRemaining == null || this.timeRemaining < 0) {
        this.timerStartTime = (new Date()).getTime();
//        console.log('bug in time remaining');
        curCrop.timerHandler = setInterval(function() {
            curCrop.grow();
        },curCrop.growTime * 1000);
    }
    else {
//        console.log('start crop timer from '+curCrop.timeRemaining);
        setTimeout(function() {
            curCrop.grow();
            curCrop.timerHandler = setInterval(function() {
                curCrop.grow();
            },curCrop.growTime * 1000);
        }, curCrop.timeRemaining);
    }
//    console.log('crop timer started');
}

Crop.prototype.grow = function() {
    this.curState += 1;

    //si on est en decay, pas la peine de re-send
    if(this.curState > this.sprites.length + 2) {
        this.decay();
        return;
    }

    this.timerStartTime = (new Date()).getTime();
    this.timeRemaining = this.getRemainingTime();

    var cropTile = findTileByRowCol(this.pos.row,this.pos.col);
        cropTile.drawTile();

    cl_crop_update(this.asSimpleObject());

//    console.log('crop grow called');
}

Crop.prototype.decay = function() {
    this.isWithering = true;

    clearInterval(this.timerHandler);
    this.timerHandler = false;

    this.curSprite = c_decay;
    var cropTile = findTileByRowCol(this.pos.row,this.pos.col);
    cropTile.drawTile();

    cl_crop_update(this.asSimpleObject());
}

Crop.prototype.updateState = function() {
    this.timeRemaining = this.getRemainingTime();
}

//retourne le temps restant pour le prochain grow() event
Crop.prototype.getRemainingTime = function() {
    return (this.growTime * 1000) - ((new Date()).getTime() - this.timerStartTime);
}

Crop.prototype.asSimpleObject = function() {
    return {
        pos:    this.pos,
        type:   this.type,
        heroId: this.heroId,
        curState:  this.curState,
        timeRemaining: this.timeRemaining
    }
}

    // --- TOMATO ---
    function Tomato(id, tile) {
        Crop.call(this, id, tile);

        this.sprites.push(c_tomato_seededImg);
        this.sprites.push(c_tomato_seedlingsImg);
        this.sprites.push(c_tomato_little_plantsImg);
        this.sprites.push(c_tomato_plantsImg);
        this.sprites.push(c_tomato_mature_plantsImg);

        this.curSprite = this.sprites[0];

        this.type = CROPS.TOMATO;

        //build price
        this.cost = 100;
        //time between grow events
        this.growTime = 10;
        //amount of harvest
        this.productivity = 20;
        //time before decaying
        this.storability = 10;
        //sell price
        this.seedPrice = 255;
    }
        Tomato.prototype = new Crop();
        Tomato.prototype.constructor = Tomato;

    // --- CORN ---
    function Corn(id, tile) {
        Crop.call(this, id, tile);

        this.sprites.push(c_corn_seededImg);
        this.sprites.push(c_corn_seedlingsImg);
        this.sprites.push(c_corn_little_plantsImg);
        this.sprites.push(c_corn_plantsImg);
        this.sprites.push(c_corn_mature_plantsImg);

        this.curSprite = this.sprites[0];

        this.type = CROPS.CORN;

        //build price
        this.cost = 150;
        //time between grow events
        this.growTime = 15;
        //amount of harvest
        this.productivity = 15;
        //time before decaying
        this.storability = 8;
        //sell price
        this.seedPrice = 320;
    }
        Corn.prototype = new Crop();
        Corn.prototype.constructor = Corn;

    // --- WHEAT ---
    function Wheat(id, tile) {
        Crop.call(this, id, tile);

        this.sprites.push(c_wheat_seededImg);
        this.sprites.push(c_wheat_seedlingsImg);
        this.sprites.push(c_wheat_little_plantsImg);
        this.sprites.push(c_wheat_plantsImg);
        this.sprites.push(c_wheat_mature_plantsImg);

        this.curSprite = this.sprites[0];

        this.type = CROPS.WHEAT;

        //build price
        this.cost = 200;
        //time between grow events
        this.growTime = 3;
        //amount of harvest
        this.productivity = 15;
        //time before decaying
        this.storability = 10;
        //sell price
        this.seedPrice = 610;
    }
        Wheat.prototype = new Crop();
        Wheat.prototype.constructor = Wheat;
